﻿
$.MGJ.MenuManage = [];

(function () {
    var _this = $.MGJ.MenuManage;
    $.extend($.MGJ.MenuManage, {
        // 新增用户
        Dialog_AddMenuObj: null,
        Dialog_AddMenu: function (obj, event) {
            var id = $(obj).attr("MenuID");
            this.Dialog_AddMenuObj = $.L.dialog({
                url: ACTION_URL("AddMenu", "Manager") + "?id=" + id,
                title: "新增菜单",
                width: 700,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#form_AddMenu").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                $.MGJ.MenuManage.Ajax_AddMenuInfo();
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                         
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },
        Dialog_EditMenuObj: null,
        Dialog_EditMenu: function (obj,event) {
            var id = $(obj).attr("MenuID");
            this.Dialog_EditMenuObj = $.L.dialog({
                url: ACTION_URL("EditMenu", "Manager")+"?id="+id,
                title: "编辑菜单",
                width: 700,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            $('#form_AddMenu').bootstrapValidator
                            var bootstrapValidator = $("#form_EditMenu").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                $.MGJ.MenuManage.Ajax_UpdateMenuInfo(id);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                          
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
            event.stopPropagation()
        },
        Dialog_CheckMenuObj: null,
        Dialog_CheckMenu: function () {
            this.Dialog_CheckMenuObj = $.L.dialog({
                url: ACTION_URL("CheckMenu", "Manager"),
                title: "查看菜单",
                width: 700,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            $.MGJ.MenuManage.Ajax_UpdateMenuInfo();
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

        }
    });
})(jQuery);





(function () {
    var _this = $.MGJ.MenuManage;
    $.extend($.MGJ.MenuManage, {
        //查询菜单
        Ajax_GetMenuInfo: function (ThisDom) {
            var ParentId;
            if (isNaN(ThisDom)) {
                ParentId = $(ThisDom).attr("MenuID");
            }
            else {
                ParentId = ThisDom;
            }
            $.L.ajaxHandle({
                url: ACTION_URL("GetMenuInfo", "Manager"),
                data: {
                    ParentId: ParentId
                },
                success: function (response) {
                    if (ParentId == 0) {
                        $("#MenuInfo").L_bindingDataList(response);

                        $(".State_TF").each(function () {
                            if ($(this).html() == "true") {
                                $(this).html("已启用")
                            } else {
                                $(this).html("已禁用")
                            }
                        })


                    } else {
                        function ThreeLevelTree(ThisDom) {
                            var ThisDomClass = ThisDom.className;
                            var NextDomClass, NextDom;
                            (ThisDomClass == "OneLevelMenu") ? NextDomClass = "TwoLevelMenu" : NextDomClass = "ThreeLevelMenu";
                            if ($(ThisDom).find(".glyphicon").hasClass("glyphicon-folder-open")) {
                                if (ThisDomClass == "OneLevelMenu") {
                                    for (var i = 0; i < response.length; i++) {
                                        NextDom += '<tr MenuID="' + response[i].ID + '" class="TwoLevelMenu" onclick="$.MGJ.MenuManage.Ajax_GetMenuInfo(this)"><td class=" text-left"><i class="glyphicon glyphicon-folder-open"></i>' + response[i].MenuName + '</td><td class="text-center">' + response[i].Controller + "/" + response[i].ActionName + '</td><td class="text-center">' + Z_TorF(response[i].StatusID) + '</td><td class="centerClo text-center"><a MenuID="' + response[i].ID + '" onclick="$.MGJ.MenuManage.Dialog_AddMenu(this, event)">添加子菜单</a> |<a MenuID="' + response[i].ID + '" onclick="$.MGJ.MenuManage.Ajax_DeleteMenuInfo(this,event)">删除</a>|<a MenuID="' + response[i].ID + '" onclick="$.MGJ.MenuManage.Dialog_EditMenu(this,event)">编辑</a></td></tr>';
                                    }
                                } else {
                                    for (var i = 0; i < response.length; i++) {
                                        NextDom += '<tr MenuID="' + response[i].ID + '" class="ThreeLevelMenu"><td class=" text-left">' + response[i].MenuName + '</td><td class="text-center">' + response[i].Controller + "/" + response[i].ActionName + '</td><td class="text-center">' + Z_TorF(response[i].StatusID) + '</td><td class="centerClo text-center"><a MenuID="' + response[i].ID + '" onclick="$.MGJ.MenuManage.Ajax_DeleteMenuInfo(this,event)">删除</a> |<a MenuID="' + response[i].ID + '" onclick="$.MGJ.MenuManage.Dialog_EditMenu(this,event)">编辑</a></td></tr>';
                                    }
                                }
                                $(ThisDom).after(NextDom).find(".glyphicon").removeClass("glyphicon-folder-open").addClass("glyphicon-file");
                            } else {
                                $(ThisDom).after(NextDom).find(".glyphicon").removeClass("glyphicon-file").addClass("glyphicon-folder-open")
                                for (var i = 0; i < 9999; i++) {
                                    if ($(ThisDom).next().attr("class") != "OneLevelMenu" && $(ThisDom).next().attr("class") != ThisDomClass) {
                                        $(ThisDom).next().remove();
                                    } else {
                                        break;
                                    }
                                }
                            }
                        }
                        ThreeLevelTree(ThisDom)
                    }
                }
            });
        },
        //新增菜单
        Ajax_AddMenuInfo: function () {
            $.L.ajaxHandle({
                url: ACTION_URL("AddMenuInfo", "Manager"),
                data: $("#form_AddMenu").serialize(),
                success: function (response) {
                    _this.Dialog_AddMenuObj.close();
                    if (response > 0) {
                        _this.Ajax_GetMenuInfo(0);
                        $.L.msgSuccess("操作成功");
                    }
                    else {
                        $.L.msgSuccess("操作失败");
                    }
                }
            });
        },
        //删除菜单
        Ajax_DeleteMenuInfo: function (obj, event) {
            $.L.msgConfirm("确认删除该记录?", function () {
                var id = $(obj).attr("MenuID");
                $.L.ajaxHandle({
                    url: ACTION_URL("DeleteMenuInfo", "Manager"),
                    data: {
                        id: id
                    },
                    success: function (response) {
                        if (response > 0) {
                            _this.Ajax_GetMenuInfo(0);
                            $.L.msgSuccess("操作成功");
                        }
                        else {
                            $.L.msgSuccess("操作失败");
                        }
                    }
                });
            
            event.stopPropagation() })

        
        },
        //编辑菜单
        Ajax_UpdateMenuInfo: function () {
            $.L.ajaxHandle({
                url: ACTION_URL("UpdateMenuInfo", "Manager"),
                data: $("#form_EditMenu").serialize(),
                success: function (response) {
                    _this.Dialog_EditMenuObj.close();
                    if (response > 0) {
                        _this.Ajax_GetMenuInfo(0);
                        $.L.msgSuccess("操作成功");
                    }
                    else {
                        $.L.msgSuccess("操作失败");
                    }
                }
            });
        }

    });
})(jQuery);
(function () {
    var _this = $.MGJ.MenuManage;
    $.extend($.MGJ.MenuManage, {
    });
})(jQuery);
function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式
    try {//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
        var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds ;
    } catch (ex) {//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
        return "";
    }
}
function Z_TorF(T_Y) {
    if (T_Y == true) {
        return "已启用"
    } else {
        return "已禁用"
    }
}





//var TwoLeveMenu = '<tr class="TwoLevelMenu" onclick="ThreeLevelTree(this)"><td class=" text-left"><i class="glyphicon glyphicon-plus"></i>11111111</td><td class="text-center">11</td><td class="text-center">111111</td><td class="text-center">111111</td><td class="centerClo text-center"><a href="javascript:delTree(0,1)">删除</a>| <a href="javascript:updTree(1)" onclick="$.MGJ.MenuManage.Dialog_EditMenu()">编辑</a>| <a href="javascript:selTreeDis(1)" onclick="$.MGJ.MenuManage.Dialog_CheckMenu()">查看</a></td></tr>'
//var ThreeLeveMenu = '<tr class="ThreeLevelMenu"><td class="text-left"> 11111111</td><td class="text-center">111111</td><td class="text-center">11</td><td class="text-center">111111</td><td class="centerClo text-center"> <a href="javascript:delTree(0,1)">删除</a>| <a href="javascript:updTree(1)" onclick="$.MGJ.MenuManage.Dialog_EditMenu()">编辑</a>| <a href="javascript:selTreeDis(1)" onclick="$.MGJ.MenuManage.Dialog_CheckMenu()">查看</a></td></tr>'


//function ThreeLevelTree(ThisDom) {
//    var ThisDomClass = ThisDom.className;
//    (ThisDomClass == "OneLevelMenu") ? NextDom = TwoLeveMenu : NextDom = ThreeLeveMenu;
//    (ThisDomClass == "OneLevelMenu") ? NextDomClass = "TwoLevelMenu" : NextDomClass = "ThreeLevelMenu";
//    if ($(ThisDom).next().attr("class") != NextDomClass) {
//        $(ThisDom).after(NextDom).find(".glyphicon").removeClass("glyphicon-plus").addClass("glyphicon-minus")
//    } else {
//        $(ThisDom).after(NextDom).find(".glyphicon").removeClass("glyphicon-minus").addClass("glyphicon-plus")
//        for (var i = 0; i < $("tr").length; i++) {
//            console.log($(ThisDom).nextAll().eq(i).attr("class"))
//            if ($(ThisDom).next().attr("class") != "OneLevelMenu" && $(ThisDom).next().attr("class") != ThisDomClass) {
//                $(ThisDom).next().remove();
//            } else {
//                break;
//            }
//        }
//    }
//}








